﻿using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using PiscoSourDominio;
using System.Collections.Generic;
using Dapper;
using System;

namespace PiscoSourSOAPServices.Persistencia
{

    public class RestauranteDAO
    {
        public string InsertarRestaurante(Restaurante restaurante)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {
                    
                    sqlConnection.Open();
                    string sqlQuery = "INSERT INTO PRS_RESTAURANTE(intCodigo,vchNombre) VALUES (@intCodigo,@vchNombre)";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodigo= restaurante.intCodigo,
                            vchNombre = restaurante.vchNombre
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ActualizarRestaurante(Restaurante restaurante)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {
                    sqlConnection.Open();
                    string sqlQuery = "UPDATE PRS_RESTAURANTE SET vchNombre=@vchNombre WHERE intCodigo=@intCodigo";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodigo = restaurante.intCodigo,
                            vchNombre = restaurante.vchNombre
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string EliminarRestaurante(Restaurante restaurante)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "DELETE FROM PRS_RESTAURANTE WHERE intCodigo=@intCodigo";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodigo = restaurante.intCodigo
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<Restaurante> ListarRestaurantes()
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                var sqlQuery = string.Format(@"Select * from PSR_RESTAURANTE");
                var result = sqlConnection.Query<Restaurante>(sqlQuery);
                sqlConnection.Close();
                return result;
            }
        }
    }
}